package org.tlipoca.smuserdirectory.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.genntii.smcommon.constant.ExceptionConstant;
import org.genntii.smcommon.context.BaseContext;
import org.genntii.smcommon.exception.UserNotFoundException;
import org.genntii.smdomain.dto.UserDirectoryDTO;
import org.genntii.smdomain.entity.UserDirectory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.tlipoca.smuserdirectory.mapper.UserDirectoryMapper;
import org.tlipoca.smuserdirectory.service.UserDirectoryService;

import java.util.ArrayList;

@Slf4j
@Service
public class UserDirectoryServiceImpl extends ServiceImpl<UserDirectoryMapper, UserDirectory> implements UserDirectoryService {

    @Autowired
    private UserDirectoryMapper userDirectoryMapper;

    @Override
    public Boolean getUserDirectoryExistsService(Long directoryId) {
        return userDirectoryMapper.exists(new QueryWrapper<UserDirectory>().eq("id", directoryId));
    }

    @Override
    public ArrayList<UserDirectory> getUserDirectoryService(Long currentId) {
        return userDirectoryMapper.selectUserDirectoryByUserId(currentId);
    }

    @Override
    public void addUserDirectoryService(UserDirectoryDTO userDirectoryDTO,Long userID) {
        userDirectoryDTO.setUserId(userID);

        userDirectoryMapper.insertUserDirectory(userDirectoryDTO);
    }

    @Override
    public void deleteUserDirectoryService(Long directoryId,Long userId) {

        userDirectoryMapper.deleteUserDirectory(directoryId, userId);
    }

    @Override
    public void deleteUserDirectoryBatchService(ArrayList<Long> directoryIds,Long userId) {

        userDirectoryMapper.deleteUserDirectoryBatch(directoryIds, userId);
    }

    @Override
    public void updateUserDirectoryService(UserDirectoryDTO userDirectoryDTO, Long directoryId, Long userId) {
        userDirectoryDTO.setUserId(userId);

        userDirectoryMapper.updateUserDirectory(userDirectoryDTO, directoryId,userId);
    }

    @Override
    public void updateUserDirectoryStatusService(Long directoryId, Integer status) {
        if(BaseContext.getCurrentId() == null) {
            throw new UserNotFoundException(ExceptionConstant.USER_NOT_FOUND_EXCEPTION);
        }

        userDirectoryMapper.updateUserDirectoryStatus(directoryId, status);
    }
}
